package com.sinopec.basemodule.widget.myedittext.clean

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.MotionEvent
import com.jelly.thor.maxnumberet.MaxEditTextView
import com.sinopec.basemodule.R
import com.sinopec.basemodule.utils.kotlin.dp2px

/**
 * 类描述：右侧是清楚按钮的最大数输入框 <br/>
 * 创建人：吴冬冬<br/>
 * 创建时间：2020/4/10 16:56 <br/>
 */
open class MaxCleanEditText :
        MaxEditTextView {

    constructor(context: Context?) : this(context, null)

    constructor(context: Context?, attrs: AttributeSet?) : super(
            context,
            attrs
    ) {
        init(attrs)
    }

    constructor(
            context: Context?,
            attrs: AttributeSet?,
            defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr) {
        init(attrs)
    }

    interface Callback {
        fun callback()
    }

    private var mCallback: Callback? = null
    fun setCallback(callback: Callback) {
        mCallback = callback
    }

    private lateinit var mShowDrawable: Drawable
    private var mShowIcWidth: Int = 18.dp2px()
    private var mShowIcHeight: Int = 18.dp2px()

    private fun init(attrs: AttributeSet?) {
        val ta = context!!.obtainStyledAttributes(attrs, R.styleable.ShowEndIconEditText)
        mShowDrawable = ta.getDrawable(R.styleable.ShowEndIconEditText_end_ic)
                ?: context.resources.getDrawable(R.drawable.icon_search_delate_normal)
        mShowIcWidth = ta.getDimension(R.styleable.ShowEndIconEditText_end_ic_width, 18.dp2px().toFloat()).toInt()
        mShowIcHeight = ta.getDimension(R.styleable.ShowEndIconEditText_end_ic_height, 18.dp2px().toFloat()).toInt()
        ta.recycle()
    }

    override fun onTextChanged(
            text: CharSequence?,
            start: Int,
            lengthBefore: Int,
            lengthAfter: Int
    ) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter)

        //具有焦点，并且文字不为空 显示清除按钮
        showClear(isFocusable && !text.isNullOrEmpty())
    }

    private fun showClear(visible: Boolean) {
        //设置显隐清除图标
        if (!::mShowDrawable.isInitialized) {
            return
        }

        mShowDrawable.setBounds(0, 0, mShowIcWidth, mShowIcWidth)
        setCompoundDrawables(
                null,
                null,
                if (visible && isEnabled) mShowDrawable else null,
                null
        )
    }

    override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect)

        //具有焦点，并且文字不为空 显示清除按钮
        showClear(isFocusable && !text.isNullOrEmpty())
    }


    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        event?.let {
            when (it.action) {
                MotionEvent.ACTION_UP -> {
                    if (isClickClear(event.x)) {
                        text = null
                        mCallback?.callback()
                    }
                }
            }
        }
        return super.onTouchEvent(event)
    }

    private fun isClickClear(x: Float): Boolean {
        // 是否触摸到清除图标区域
        return compoundDrawables[2] != null && x >= width - compoundPaddingRight && x <= width
    }
}